﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using DigtiMatrix.Entity;
using DigitMatrix.DataAccess;
using log4net;
using System.IO;
using System.Reflection;
using System.Transactions;
namespace DigitMatrix.Business
{
    public class DeviceBusiness
    {
        public static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private static DeviceBusiness instance;
        public static DeviceBusiness Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new DeviceBusiness();
                }
                return instance;
            }
        }

        
        public DeviceBusiness()
        {
             
        }
        public int Insert(ref string errMessage, DeviceInfo oDeviceInfo)
        {
            Database db = DatabaseFactory.CreateDatabase();
            errMessage = "";
            try
            {
              
                 
                return DeviceDataAccess.Insert(db, oDeviceInfo);

            }
            catch (Exception ex)
            {
                errMessage = ex.Message + ex.StackTrace;
                logger.Error("Error Message:" + ex.Message + " Trace:" + ex.StackTrace);
                return -1;
            }
        }
        public int Insert(ref string errMessage, List<CamercaInfo> listCam, DeviceInfo oDeviceInfo)
        {
            Database db = DatabaseFactory.CreateDatabase();
            errMessage = "";
            int iDeviceId = 0;
            int iRtn = 0;
            try
            {
               // using (TransactionScope transaction = new TransactionScope())
                //{
                    iRtn=  DeviceDataAccess.Insert(db, oDeviceInfo);
                    iDeviceId = DeviceDataAccess.GetMaxDeviceId(db);
                    foreach (CamercaInfo  item in listCam)
                    {
                        item.DeviceId = iDeviceId;
                        CamercaDataAccess.Insert(db, item);
                    }
                  //  transaction.Complete();
               // }
                return iRtn;

            }
            catch (Exception ex)
            {
                errMessage = ex.Message + ex.StackTrace;
                logger.Error("Error Message:" + ex.Message + " Trace:" + ex.StackTrace);
                return -1;
            }
        }
        public int Update(ref string errMessage, List<CamercaInfo> listCam, DeviceInfo oDeviceInfo)
        {
            Database db = DatabaseFactory.CreateDatabase();
            errMessage = "";
            int iRtn = -1;
            try
            {
                CamercaDataAccess.DeleteByDeviceId(db, oDeviceInfo.DeviceId);
                foreach (CamercaInfo item in listCam)
                {
                    CamercaDataAccess.Insert(db, item);
                }
                iRtn = DeviceDataAccess.Update(db, oDeviceInfo);
                return iRtn;

            }
            catch (Exception ex)
            {
                errMessage = ex.Message + ex.StackTrace;
                logger.Error("Error Message:" + ex.Message + " Trace:" + ex.StackTrace);
                return -1;
            }


        }

        public int Update(ref string errMessage, DeviceInfo oDeviceInfo)
        {
            Database db = DatabaseFactory.CreateDatabase();
            errMessage = "";
            try
            {
                return DeviceDataAccess.Update(db, oDeviceInfo);

            }
            catch (Exception ex)
            {
                errMessage = ex.Message + ex.StackTrace;
                logger.Error("Error Message:" + ex.Message + " Trace:" + ex.StackTrace);
                return -1;
            }


        }
        public int Delete(ref string errMessage, int deviceId)
        {
            Database db = DatabaseFactory.CreateDatabase();
            errMessage = "";
            try
            {
                int iRtn= DeviceDataAccess.Delete(db, deviceId);
                iRtn = CamercaDataAccess.DeleteByDeviceId(db, deviceId);
                return iRtn;
            }
            catch (Exception ex)
            {
                errMessage = ex.Message + ex.StackTrace;
                logger.Error("Error Message:" + ex.Message + " Trace:" + ex.StackTrace);
                return -1;
            }

        }

        public Dictionary<int,DeviceInfo> GetAllDeviceInfo(ref string errMessage)
        {
            Database db = DatabaseFactory.CreateDatabase();
            errMessage = "";
            Dictionary<int, DeviceInfo> list = new Dictionary<int, DeviceInfo>();
            try
            {
                DeviceInfo oDeviceInfo;
                DataSet ds = DeviceDataAccess.GetAllDeviceInfo(db);
                DataSet dsCamerca;
                CamercaInfo oCamerca;
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                        oDeviceInfo = new DeviceInfo(ds.Tables[0].Rows[i]);

                        oDeviceInfo.ListCamerca = new Dictionary<int, CamercaInfo>();
                      
                        dsCamerca = CamercaDataAccess.GetCamInfoByDeviceId(db, oDeviceInfo.DeviceId);
                        foreach (DataRow drCam in dsCamerca.Tables[0].Rows)
                        {
                            oCamerca=new CamercaInfo(drCam);
                            oDeviceInfo.ListCamerca.Add(oCamerca.CamercaId, oCamerca);
                        }
                        list.Add(oDeviceInfo.DeviceId, oDeviceInfo);

                   
                }
                return list;

            }
            catch (Exception ex)
            {
                errMessage = ex.Message + ex.StackTrace;
                logger.Error("Error Message:" + ex.Message + " Trace:" + ex.StackTrace);
                return null;
            }
        }
        public Dictionary<int, DeviceInfo> GetDeviceInfoByGroupId(ref string errMessage, int GroupId)
        {
            Database db = DatabaseFactory.CreateDatabase();
            errMessage = "";
            try
            {
                Dictionary<int, DeviceInfo> list = new Dictionary<int, DeviceInfo>();
                DataSet ds = DeviceDataAccess.GetDeviceInfoByGroupId(db, GroupId);
                if (ds.Tables[0].Rows.Count == 0)
                {
                    return null;
                }
                DataSet dsCamerca;
                CamercaInfo oCamerca;
                DeviceInfo oDeviceInfo;
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    oDeviceInfo = new DeviceInfo(ds.Tables[0].Rows[i]);

                    oDeviceInfo.ListCamerca = new Dictionary<int, CamercaInfo>();

                    dsCamerca = CamercaDataAccess.GetCamInfoByDeviceId(db, oDeviceInfo.DeviceId);
                    foreach (DataRow drCam in dsCamerca.Tables[0].Rows)
                    {
                        oCamerca = new CamercaInfo(drCam);
                        oDeviceInfo.ListCamerca.Add(oCamerca.CamercaId, oCamerca);
                    }
                    list.Add(oDeviceInfo.DeviceId, oDeviceInfo);

                }
                return list;

            }
            catch (Exception ex)
            {
                errMessage = ex.Message + ex.StackTrace;
                logger.Error("Error Message:" + ex.Message + " Trace:" + ex.StackTrace);
                return null;
            }
        }
        


        public DeviceInfo GetDeviceInfoByDeviceId(ref string errMessage, int DeviceId)
        {
            Database db = DatabaseFactory.CreateDatabase();
            errMessage = "";
            try
            {
                DataSet ds = DeviceDataAccess.GetDeviceInfoByDeviceId(db, DeviceId);
                if (ds.Tables[0].Rows.Count == 0)
                {
                    return null;
                }
                DataSet dsCamerca;
                CamercaInfo oCamerca;

                DeviceInfo oDeviceInfo = new DeviceInfo(ds.Tables[0].Rows[0]);
               oDeviceInfo = new DeviceInfo(ds.Tables[0].Rows[0]);
               oDeviceInfo.ListCamerca = new Dictionary<int, CamercaInfo>();
                dsCamerca = CamercaDataAccess.GetCamInfoByDeviceId(db, oDeviceInfo.DeviceId);
                foreach (DataRow drCam in dsCamerca.Tables[0].Rows)
                {
                    oCamerca = new CamercaInfo(drCam);
                    oDeviceInfo.ListCamerca.Add(oCamerca.CamercaId, oCamerca);
                }
                return oDeviceInfo;

            }
            catch (Exception ex)
            {
                errMessage = ex.Message + ex.StackTrace;
                logger.Error("Error Message:" + ex.Message + " Trace:" + ex.StackTrace);
                return null;
            }
        }

    }
}
